//
//  XJYLandingNextViewController.m
//  ShopMall
//
//  Created by XJY on 17/1/18.
//  Copyright © 2017年 XinJinYing. All rights reserved.
//

#import "XJYLandingNextViewController.h"
#import "XJYLandingNextView.h"

@interface XJYLandingNextViewController ()

@property (nonatomic, strong) XJYLandingNextView *landingNextView;

@end

@implementation XJYLandingNextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = MainColor;
    self.title = @"验证手机号";
    self.edgesForExtendedLayout = 0;
    
    [self.view addSubview:self.landingNextView];
  
    [self setupLayout];
    //进入页面后先请求手机号是否已经注册过
    [self requestCodeNumber];
}

#pragma mark - 布局
- (void)setupLayout
{
    __weak typeof(self) weakSelf = self;
    [_landingNextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.left.equalTo(weakSelf.view);
        make.height.equalTo(@200);
    }];

}

#pragma mark - 懒加载
- (XJYLandingNextView *)landingNextView
{
    if (!_landingNextView) {
        _landingNextView = [[XJYLandingNextView alloc] init];
        _landingNextView.phoneNum = self.messageDic[@"username"];
        
        __weak typeof(self) weakSelf = self;
        _landingNextView.landingBlock = ^(NSString *code) {
            [weakSelf landingMethod:code];
        };
    }
    return _landingNextView;
}

#pragma mark - 请求方法
//将验证码提交到后台注册
- (void)landingMethod:(NSString *)code
{
    NSString *path = @"appMember/appRegistration.do";
    NSDictionary *dict = @{@"LoginName" : _messageDic[@"username"],
                           @"Lpassword":_messageDic[@"password"],
                           @"Code":code,
                           @"Telephone":_messageDic[@"username"]};
    [self getWithPath:path params:dict success:^(id json) {
        if ([json[@"rsult"] isEqualToString:@"success"]) {
            [self showTostMessage:@"注册成功"];
            [self autoLogin];
        } else if ([json[@"result"] isEqualToString:@"codeError"]) {
            [self showTostMessage:@"验证码错误"];
        } else {
            [self showTostMessage:@"注册失败"];
        }
        XJYLog(@"responseObject : %@", json);
    } failure:^(NSError *error) {
        XJYLog(@"error : %@", error);
    }];
}

//发送验证码
- (void)requestCodeNumber
{
    NSString *path = @"appMember/createCode.do";
    NSDictionary *param = @{@"MemberId":_messageDic[@"username"]};
    [self postWithPath:path params:param success:^(id json) {
        XJYLog(@"responseObject : %@", json);
        if ([json[@"result"] isEqualToString:@"success"]) {
            [self.landingNextView gcdTimer];
        } else if ([json[@"result"] isEqualToString:@"TelephoneExistError"]) {
            [self showTostMessage:@"手机号已经被注册"];
        } else {
            [self showTostMessage:@"验证码请求失败"];
        }
        
    } failure:^(NSError *error) {
        XJYLog(@"error : %@", error);
    }];
}

//登录方法
- (void)autoLogin
{
    NSString *path = @"appMember/appLogin.do";
    NSDictionary *params = @{@"LoginName":_messageDic[@"username"],@"Lpassword":_messageDic[@"password"]};
    [self getWithPath:path params:params success:^(id json) {
        XJYLog(@"responseObject : %@", json);
        if ([json[@"result"] isEqualToString:@"0"]) {
            [[NSUserDefaults standardUserDefaults] setObject:json forKey:@"isLogin"];
            [self performSelector:@selector(popRootViewController) withObject:nil afterDelay:0];
        }
    } failure:^(NSError *error) {
        XJYLog(@"error : %@", error);
    }];
}

- (void)popRootViewController
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
